var circleID = $.getUrlVar("id");
var forword = encodeURIComponent(window.location.href);
var backUrl = $.getUrlVar("back");
var info = new Vue({
    el: "#app",
    data: {
        cirDetail: {},
        ansList: [],
        isLoading: true,
        pageIndex: 1,
        pageSize: PAGESIZE,
        tabs: 0,
        reqOut:false, //判断请求数据是否已完
        screenSide: true,
        addShow: false,

        inputText: '',
        showModal: false,
        shareObj: {
            dataID: '',
            shareTit: '',
            sharePic: ''
        },
        searchkey:'',
    },
    created: function() {
        var dtd = $.Deferred();
        var _self = this;
        // 列表
        var data = {
            circleID: circleID
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({
            url: 'circle_getCircleDetail',
            datatype: 'json',
            type: 'get',
            data: data
        }, function(data) {
            if (data.error_code == 0) {
                _self.cirDetail = data.data[0];
            } else {
                alert(data.msg);
            }
        });

        //查看上次是否有Tab记录的先
        var backoperation = $.getCookie('backoperation');
        if(backoperation && typeof(backoperation) === 'string' && backoperation.length > 0)
        {
            $.setCookie('backoperation', "");
            //查看上次所保存的tab
            var _tabs = $.getCookie('circleListtabs');
            if(_tabs)
            {
                _self.tabs = parseInt(_tabs) > 2?0:parseInt(_tabs);
            }
        }

        _self.slides(_self.tabs);
        //_self.loadlist(_self.tabs);
    },
    compiled: function() {
        var _self = this;
        $(document).scroll(function() {
            if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
                if(!_self.reqOut){
                    _self.reqOut = true;
                    var str = '<img class="loading-img" src="images/loading.gif"></img>';
                    myLoad.pullLoad('quesCont',str);
                    _self.loadlist(_self.tabs);
               
                }
            }

        });

        $(document).on('change.inputText', '.fillInput', function() {
            _self.inputText = $(this).val();
        });

        // 比例
        myJS.imgScale('.j_1', '1', '1');
        myJS.imgScale('.j_6', '6', '5');
        myJS.ImgFull('.loadImg','/images/liby.png');
    },
    methods: {
        loadlist: function(tabs) {
            var _self = this;
            var vModal = _self, target;
            switch (tabs) {
                case 0:
                    _self.screenSide = true;
                    var url = 'circle_getTopicList';
                    var data = {
                        pageSize: _self.pageSize,
                        pageIndex: _self.pageIndex,
                        sortType: '',
                        circleID: circleID
                    }
                    vModal = _self;
                    target = _self.ansList;
                    break;
                case 1:
                    _self.screenSide = true;
                    var url = 'circle_getTopicList';
                    var data = {
                        pageSize: _self.pageSize,
                        pageIndex: _self.pageIndex,
                        type: '2',
                        sortType: '1',
                        circleID: circleID
                    }
                    vModal = _self;
                    target = _self.ansList;
                    break;
                case 2:
                    _self.screenSide = true;
                    var url = 'circle_getTopicList';
                    var data = {
                        pageSize: _self.pageSize,
                        pageIndex: _self.pageIndex,
                        sortType: '1',
                        circleID: circleID
                    }
                    vModal = _self;
                    target = _self.ansList;
                    break;
                default:
                    break;
            }
            if (!vModal.isLoading) return;
            data = Common.jsonEncode(data);
            Common.ajaxCall({
                url: url,
                datatype: 'json',
                type: 'post',
                data: data
            }, function(data) {
                if (data.error_code == 0 && data.data.length > 0) {
                    if (vModal.pageIndex == 1) {
                        target.length = 0;
                    }
                    for (var i = 0; i < data.data.length; i++) {
                        target.push(data.data[i]);
                    }
                    _self.reqOut = false; 
                    myLoad.pullHide();//加载后，把效果去掉
                } else {
                    _self.reqOut = true;
                    myLoad.noDate();//暂无数据
                    vModal.isLoading = false;
                }
                
                vModal.pageIndex++;
            });
        },
        slides: function(index, e) {
            var _self = this;
            _self.tabs = index;
            $.setCookie('circleListtabs' , index);//保存最近的Tab
            if(e && typeof(e) != 'undefined')
            {
                $(e.currentTarget).addClass('on').siblings().removeClass('on');
            }            
            _self.ansList = []; //清空上一次请求vm储存的数据
            _self.reqOut = false;
            var data = {
                isLoading: true,
                pageSize: PAGESIZE,
                pageIndex: 1
            };
            _self = $.extend(_self, data);
            _self.loadlist(_self.tabs);
        },
        loadPhoto: function() {
            // 比例
            myJS.imgScale('.j_1', '1', '1');
            myJS.imgScale('.j_6', '6', '5');
            myJS.ImgFull('.loadImg','/images/liby.png');
        },
        add: function() {
            if (this.addShow == false) {
                this.addShow = true;
            } else {
                this.addShow = false;
            }
        },
        send: function() {
            location.href = "publish.html?id=" + circleID + "&back=" + encodeURIComponent(location.href);
        },
        joinCircle: function(typeID, circleID, e) {
            var _self = this;
            //圈子加入
            var data = {
                circleID: circleID,
                typeID: typeID
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({
                url: 'circle_addCircle',
                datatype: 'json',
                type: 'get',
                data: data
            }, function(data) {
                var $numberIcon = $('.numberIcon'),
                    number = parseInt($numberIcon.text());
                if (data.error_code == 0) {
                    if (typeID === 1) {
                        _self.cirDetail.isJoin = 1;
                        $numberIcon.text(number + 1);
                    } else {
                        _self.cirDetail.isJoin = 0;
                        $numberIcon.text(number - 1);
                    }

                } else {
                    alert(data.msg);
                }
            });
        },
        swipeLeft: function(e) {
            var _self = this;
            if (_self.isEdit === '取消') return; //如果处于编辑状态则不予左滑

            var shareColl = $(e.target).parents('li');
            var touchDrift = ($(e.target).parents('li').width()) / 3;
            var drift = ($(e.target).parents('li').width()) / 3;
            //回复原位置
            _self.isDrift = false; //标识为非左滑状态
            $(e.target).parents('ul').children().animate({
                left: 0
            });
            $(e.target).parents('ul').children().find('.shareColl').animate({
                width: 0,
                right: 0
            });


            console.log(e.deltaX);
            if (e.deltaX < 0) { //左滑
                _self.isDrift = true; //标识为左滑状态
                if (e.deltaX < -touchDrift) {
                    $(e.target).parents('li').animate({
                        left: -drift
                    });
                    var height = $(e.target).parents('li').outerHeight();
                    shareColl.find('.shareColl').animate({
                        width: drift,
                        right: -drift
                    }).show().css('line-height', height + 'px');
                }
            } else { //右滑
                if (e.deltaX > touchDrift) {
                    _self.isDrift = false; //标识为非左滑状态
                    $(e.target).parents('li').animate({
                        left: 0
                    });
                    shareColl.find('.shareColl').fadeOut();

                }
            }
        },
        share: function(obj, e) {
            $(e.target).parents('li').animate({
                left: 0
            }).find('.shareColl').fadeOut(); //将左滑的元素复位
            var _self = this;
            _self.showModal = true;
            console.log(obj);
            _self.shareObj = {
                dataID: obj.shareId, //文档id
                shareTit: obj.shareTit, //文档标题
                sharePic: obj.sharePic //文档图片
            };

        },
        shareReg: function(userList) {
            var _self = this;
            if (!userList) { //如果没选择分享对象回到原来界面
                _self.showModal = false;
                return;
            }

            alert({
                close: false,
                text: '分享',
                description: '<div class="clearfix shareTxt"><span class="j_1 photo"><img class="s_1" src="' + _self.shareObj.sharePic + '" onerror=\"javascript:this.src=\'images/icon/viewIcon.png\';this.onerror=null;\"></span><span class="text">' + _self.shareObj.shareTit + '</span></div>',
                fillInput: true,
                kind: 2,
                okEvent: function() {
                    var data = {
                        dataID: _self.shareObj.dataID,
                        userList: userList,
                        comment: _self.inputText,
                        type: 2
                    };
                    data = Common.jsonEncode(data);
                    Common.ajaxCall({
                        url: 'look_share',
                        datatype: 'json',
                        type: 'get',
                        data: data
                    }, function(data) {
                        if (data.error_code == 0) {
                            var obj = data.data[0];
                            var code = obj.code;
                            var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                            alert({
                                text: '<img src="images/' + pic + '"/>',
                                description: obj.str,
                                kind: 0
                            });
                        } else {
                            alert(data.msg);
                        }
                    });
                }
            });
        },
        //收藏
        Collection: function(obj, e) {
            var _self = this;
            _self.addShow = false;
            $(e.target).parents('li').animate({
                left: 0
            }).find('.shareColl').fadeOut(); //将左滑的元素复位
            var data = {
                dataID: obj.dataID,
                type: 2,
                isCollection: 1
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({
                url: 'look_addCollection',
                datatype: 'json',
                type: 'get',
                data: data
            }, function(data) {
                if (data.error_code == 0) {
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                    alert({
                        text: '<img src="images/' + pic + '"/>',
                        description: obj.str,
                        kind: 0
                    });
                } else {
                    alert(data.msg);
                }
            });
        },
        back: function() {
            window.location.href = decodeURIComponent(backUrl);
        },
        toDetail: function(id) {
            window.location.href = "circleDetail.html?id=" + id + "&back=" + forword;
        }

    },
    watch: {
        "cirDetail": function() {
            this.loadPhoto();
        },
        "ansList": function() {
            this.loadPhoto();
        }
    }
});